using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using LibNDSFormats.NSBMD;
using System.IO;
using Tao.OpenGl;
using OpenTK;
using MyExtensions;

namespace MKDS_Course_Editor.MKDSEditor
{
    public partial class CourseEditor : Form
    {
        string path;
        string path2;
        string path3;
        int course_id;
        public CourseEditor(string path, string path2, string path3, int course_id)
        {
            InitializeComponent();
            simpleOpenGlControl1.Tag = false;
            this.path = path;
            this.path2 = path2;
            this.path3 = path3;
            this.course_id = course_id;
            simpleOpenGlControl1.InitializeContexts();
            simpleOpenGlControl1.MouseWheel += new MouseEventHandler(simpleOpenGlControl1_MouseWheel);
        }
        void simpleOpenGlControl1_MouseWheel(object sender, MouseEventArgs e)
        {
            dist += (float)e.Delta / 1000;
            simpleOpenGlControl1.Refresh();
            //RenderFunc1();
        }
        List<NSBTA.NSBTA.NSBTA_File> ani = new List<NSBTA.NSBTA.NSBTA_File>();
        List<Nsbmd> nsbm = new List<Nsbmd>();
        List<Vector3> pos = new List<Vector3>();
        List<Vector3> rot = new List<Vector3>();
        List<Vector3> scale = new List<Vector3>();
        List<bool> tt = new List<bool>();
        List<object> obj = new List<object>();
        List<NsbmdGlRenderer> t = new List<NsbmdGlRenderer>();
        MKDS_Course_Editor.nkm.nkmFile m;
        private void CourseEditor_Load(object sender, EventArgs e)
        {
			//Gl.glEnable(Gl.GL_LIGHT0);
			Gl.glEnable(Gl.GL_RESCALE_NORMAL);
			Gl.glEnable(Gl.GL_COLOR_MATERIAL);
			Gl.glEnable(Gl.GL_DEPTH_TEST);
			Gl.glDisable(Gl.GL_CULL_FACE);
			Gl.glFrontFace(Gl.GL_CCW);
			Gl.glEnable(Gl.GL_TEXTURE_2D);
			Gl.glClearDepth(1);
			Gl.glEnable(Gl.GL_ALPHA_TEST);
			Gl.glBlendFunc(Gl.GL_SRC_ALPHA, Gl.GL_ONE_MINUS_SRC_ALPHA);
			Gl.glEnable(Gl.GL_BLEND);
			Gl.glAlphaFunc(Gl.GL_GREATER, 0f);
			Gl.glClearColor(51f / 255f, 51f / 255f, 51f / 255f, 1f);
            ReadCourseModel();
            ReadVModel();
            ReadNKM();
            ParseNKM();

            simpleOpenGlControl1.Refresh();
            //RenderFunc1();
        }
        void ReadCourseModel()
        {
            nsbm.Add(NsbmdLoader.LoadNsbmd(File.OpenRead(path + "\\" + "unpack\\" + "course_model.nsbmd")));
            obj.Add(null);
            pos.Add(new Vector3(0, 0, 0));
            scale.Add(new Vector3(1, 1, 1));
            rot.Add(new Vector3(0, 0, 0));
            t.Add(new NsbmdGlRenderer());
            tt.Add(true);
            if (File.Exists(path2 + "\\" + "unpack\\" + "course_model.nsbtx"))
            {
                nsbm[0].materials = LibNDSFormats.NSBTX.NsbtxLoader.LoadNsbtx(File.OpenRead(path2 + "\\" + "unpack\\" + "course_model.nsbtx"), out nsbm[0].Textures, out nsbm[0].Palettes);
            }
            try
            {
                nsbm[0].MatchTextures();
            }
            catch { }
            /*for (int i = 0; i < nsbm.models[0].Polygons.Count; i++)
            {
                TreeNode q = new TreeNode(nsbm.models[0].Polygons[i].Name);
                q.Checked = true;
                treeView1.Nodes.Add(q);
            }*/
            if (File.Exists(path + "\\" + "unpack\\" + "course_model.nsbta"))
            {
                ani.Add(NSBTA.NSBTA.Read(path + "\\" + "unpack\\" + "course_model.nsbta"));
                aniframeS.Add(new int[ani[0].MAT.num_objs]);
                aniframeT.Add(new int[ani[0].MAT.num_objs]);
                aniframeR.Add(new int[ani[0].MAT.num_objs]);
                aniScaleS.Add(new int[ani[0].MAT.num_objs]);
                aniScaleT.Add(new int[ani[0].MAT.num_objs]);
                //timer1.Enabled = true;
            }
            else
            {
                ani.Add(new NSBTA.NSBTA.NSBTA_File());
                aniframeS.Add(new int[0]);
                aniframeT.Add(new int[0]);
                aniframeR.Add(new int[0]);
                aniScaleS.Add(new int[0]);
                aniScaleT.Add(new int[0]);
            }
            //LevelModel = Gl.glGenLists(1);
            //Gl.glNewList(LevelModel, Gl.GL_COMPILE);
            //t[0].Model = nsbm[0].models[0];
            //t[0].RenderModel("", ani[0], aniframeS[0], aniframeT[0], aniScaleS[0], aniScaleT[0], aniframeR[0], new NSBCA.NSBCA.NSBCA_File(), false, -1, X, Y, dist, elev, ang);
            //Gl.glEndList();
        }
        void ReadVModel()
        {
            if (File.Exists(path + "\\" + "unpack\\" + "course_model_V.nsbmd"))
            {
                nsbm.Add(NsbmdLoader.LoadNsbmd(File.OpenRead(path + "\\" + "unpack\\" + "course_model_V.nsbmd")));
                pos.Add(new Vector3(0, 0, 0));
                obj.Add(null);
                scale.Add(new Vector3(1f, 1f, 1f));
                rot.Add(new Vector3(0, 0, 0));
                tt.Add(true);
                t.Add(new NsbmdGlRenderer(nsbm[0].models[0].Materials.Count));
                if (File.Exists(path2 + "\\" + "unpack\\" + "course_model_V.nsbtx"))
                {
                    nsbm[1].materials = LibNDSFormats.NSBTX.NsbtxLoader.LoadNsbtx(File.OpenRead(path2 + "\\" + "unpack\\" + "course_model_V.nsbtx"), out nsbm[1].Textures, out nsbm[1].Palettes);
                }
                try
                {
                    nsbm[1].MatchTextures();
                }
                catch { }
                /*for (int i = 0; i < nsbm.models[0].Polygons.Count; i++)
                {
                    TreeNode q = new TreeNode(nsbm.models[0].Polygons[i].Name);
                    q.Checked = true;
                    treeView1.Nodes.Add(q);
                }*/
                if (File.Exists(path + "\\" + "unpack\\" + "course_model_V.nsbta"))
                {
                    ani.Add(NSBTA.NSBTA.Read(path + "\\" + "unpack\\" + "course_model_V.nsbta"));
                    aniframeS.Add(new int[ani[1].MAT.num_objs]);
                    aniframeT.Add(new int[ani[1].MAT.num_objs]);
                    aniframeR.Add(new int[ani[1].MAT.num_objs]);
                    aniScaleS.Add(new int[ani[1].MAT.num_objs]);
                    aniScaleT.Add(new int[ani[1].MAT.num_objs]);
                    //timer1.Enabled = true;
                }
                else
                {
                    ani.Add(new NSBTA.NSBTA.NSBTA_File());
                    aniframeS.Add(new int[0]);
                    aniframeT.Add(new int[0]);
                    aniframeR.Add(new int[0]);
                    aniScaleS.Add(new int[0]);
                    aniScaleT.Add(new int[0]);
                }
                //SkyModel = Gl.glGenLists(1);
                //Gl.glNewList(SkyModel, Gl.GL_COMPILE);
                //t[1].Model = nsbm[1].models[0];
                //t[1].RenderModel("", ani[1], aniframeS[1], aniframeT[1], aniScaleS[1], aniScaleT[1], aniframeR[1], new NSBCA.NSBCA.NSBCA_File(), false, -1, X, Y, dist, elev, ang);
                //Gl.glEndList();
            }
        }
        void ReadNKM()
        {
            EndianBinaryReader er = new EndianBinaryReader(File.OpenRead(path + "\\" + "unpack\\" + "course_map.nkm"), Endianness.LittleEndian);
            m = new MKDS_Course_Editor.nkm.nkmFile();
            m.Header.Type = er.ReadString(Encoding.ASCII, 4);
            if (m.Header.Type == "NKMD")
            {
                m.Header.Unknown1 = er.ReadInt16();
                m.Header.HeaderLength = er.ReadInt16();
                m.Header.SectionOffset = new Int32[(m.Header.HeaderLength - 8) / 4];
                for (int i = 0; i < m.Header.SectionOffset.Length; i++)
                {
                    m.Header.SectionOffset[i] = er.ReadInt32();
                }
                for (int i = 0; i < m.Header.SectionOffset.Length; i++)
                {
                    er.BaseStream.Position = m.Header.SectionOffset[i] + m.Header.HeaderLength;
                    string id = er.ReadString(Encoding.ASCII, 4);
                    switch (id)
                    {
                        case "OBJI":
                            m.OBJI.ID = id;
                            m.OBJI.NrItem = er.ReadInt32();
                            m.OBJI.Items = new List<MKDS_Course_Editor.nkm.OBJi>();
                            for (int j = 0; j < m.OBJI.NrItem; j++)
                            {
                                ListViewItem n = new ListViewItem((j + 1).ToString());
                                m.OBJI.Items.Add(new MKDS_Course_Editor.nkm.OBJi());
                                m.OBJI.Items[j].TransX = (float)(er.ReadInt32()) / 4096f;
                                m.OBJI.Items[j].TransY = (float)(er.ReadInt32()) / 4096f;
                                m.OBJI.Items[j].TransZ = (float)(er.ReadInt32()) / 4096f;
                                m.OBJI.Items[j].RotX = (float)(er.ReadInt32()) / 4096f;
                                m.OBJI.Items[j].RotY = (float)(er.ReadInt32()) / 4096f;
                                m.OBJI.Items[j].RotZ = (float)(er.ReadInt32()) / 4096f;
                                m.OBJI.Items[j].ScaleX = (float)(er.ReadInt32()) / 4096f;
                                m.OBJI.Items[j].ScaleY = (float)(er.ReadInt32()) / 4096f;
                                m.OBJI.Items[j].ScaleZ = (float)(er.ReadInt32()) / 4096f;
                                m.OBJI.Items[j].ObjectID = er.ReadBytes(2);
                                m.OBJI.Items[j].RouteID = er.ReadBytes(2);
                                m.OBJI.Items[j].Setting1 = er.ReadBytes(4);
                                m.OBJI.Items[j].Setting2 = er.ReadBytes(4);
                                m.OBJI.Items[j].Setting3 = er.ReadBytes(4);
                                m.OBJI.Items[j].Setting4 = er.ReadBytes(4);
                                m.OBJI.Items[j].Setting5 = er.ReadBytes(4);
                                n.SubItems.Add(m.OBJI.Items[j].TransX.ToString());
                                n.SubItems.Add(m.OBJI.Items[j].TransY.ToString());
                                n.SubItems.Add(m.OBJI.Items[j].TransZ.ToString());
                                n.SubItems.Add(m.OBJI.Items[j].RotX.ToString());
                                n.SubItems.Add(m.OBJI.Items[j].RotY.ToString());
                                n.SubItems.Add(m.OBJI.Items[j].RotZ.ToString());
                                n.SubItems.Add(m.OBJI.Items[j].ScaleX.ToString());
                                n.SubItems.Add(m.OBJI.Items[j].ScaleY.ToString());
                                n.SubItems.Add(m.OBJI.Items[j].ScaleZ.ToString());
                                n.SubItems.Add(System.BitConverter.ToString(m.OBJI.Items[j].ObjectID).Replace("-", "") + System.BitConverter.ToString(m.OBJI.Items[j].RouteID).Replace("-", ""));
                                n.SubItems.Add(System.BitConverter.ToString(m.OBJI.Items[j].Setting1).Replace("-", ""));
                                n.SubItems.Add(System.BitConverter.ToString(m.OBJI.Items[j].Setting2).Replace("-", ""));
                                n.SubItems.Add(System.BitConverter.ToString(m.OBJI.Items[j].Setting3).Replace("-", ""));
                                n.SubItems.Add(System.BitConverter.ToString(m.OBJI.Items[j].Setting4).Replace("-", ""));
                                n.SubItems.Add(System.BitConverter.ToString(m.OBJI.Items[j].Setting5).Replace("-", ""));
                                //listView1.Items.Add(n);
                                //chart1.Series[0].Points.AddXY(m.OBJI.Items[j].TransX, m.OBJI.Items[j].TransZ);
                                if (Properties.Resources.ResourceManager.GetObject("OBJ_" + BitConverter.ToString(m.OBJI.Items[j].ObjectID).Replace("-", "") + ((m.OBJI.Items[j].ObjectID[0] == 0x6D && m.OBJI.Items[j].ObjectID[1] == 0x00) == false ? "" : "_" + BitConverter.ToInt32(m.OBJI.Items[j].Setting1, 0))) != null)
                                {
                                    //nkmEditorControl1.OBJI.Add(m.OBJI.Items[j].TransX, m.OBJI.Items[j].TransY, m.OBJI.Items[j].TransZ, m.OBJI.Items[j].RotY, (Bitmap)Properties.Resources.ResourceManager.GetObject("OBJ_" + BitConverter.ToString(m.OBJI.Items[j].ObjectID).Replace("-", "") + ((m.OBJI.Items[j].ObjectID[0] == 0x6D && m.OBJI.Items[j].ObjectID[1] == 0x00) == false ? "" : "_" + BitConverter.ToInt32(m.OBJI.Items[j].Setting1, 0))));
                                }
                                else
                                {
                                    //chart1.Series[0].Points[j].MarkerColor = Color.Red;
                                    //nkmEditorControl1.OBJI.Add(m.OBJI.Items[j].TransX, m.OBJI.Items[j].TransY, m.OBJI.Items[j].TransZ, m.OBJI.Items[j].RotY);
                                }
                            }
                            break;
                        case "PATH":
                            m.PATH.ID = id;
                            m.PATH.NrItem = er.ReadInt32();
                            m.PATH.Items = new List<MKDS_Course_Editor.nkm.PATh>();
                            for (int j = 0; j < m.PATH.NrItem; j++)
                            {
                                ListViewItem n = new ListViewItem((j + 1).ToString());
                                m.PATH.Items.Add(new MKDS_Course_Editor.nkm.PATh());
                                m.PATH.Items[j].NrPoit = er.ReadBytes(4);
                                n.SubItems.Add(System.BitConverter.ToString(m.PATH.Items[j].NrPoit).Replace("-", ""));
                                //listView2.Items.Add(n);
                            }
                            break;
                        case "POIT":
                            m.POIT.ID = id;
                            m.POIT.NrItem = er.ReadInt32();
                            m.POIT.Items = new List<MKDS_Course_Editor.nkm.POIt>();
                            for (int j = 0; j < m.POIT.NrItem; j++)
                            {
                                ListViewItem n = new ListViewItem((j + 1).ToString());
                                m.POIT.Items.Add(new MKDS_Course_Editor.nkm.POIt());
                                m.POIT.Items[j].X = (float)(er.ReadInt32()) / 4096f;
                                m.POIT.Items[j].Y = (float)(er.ReadInt32()) / 4096f;
                                m.POIT.Items[j].Z = (float)(er.ReadInt32()) / 4096f;
                                m.POIT.Items[j].Nr = er.ReadByte();
                                m.POIT.Items[j].Setting1 = er.ReadBytes(3);
                                m.POIT.Items[j].Setting2 = er.ReadBytes(4);
                                n.SubItems.Add(m.POIT.Items[j].X.ToString());
                                n.SubItems.Add(m.POIT.Items[j].Y.ToString());
                                n.SubItems.Add(m.POIT.Items[j].Z.ToString());
                                n.SubItems.Add(m.POIT.Items[j].Nr.ToString());
                                n.SubItems.Add(System.BitConverter.ToString(m.POIT.Items[j].Setting1).Replace("-", ""));
                                n.SubItems.Add(System.BitConverter.ToString(m.POIT.Items[j].Setting2).Replace("-", ""));
                                //listView3.Items.Add(n);
                                //nkmEditorControl1.POIT.Add(m.POIT.Items[j].X, m.POIT.Items[j].Y, m.POIT.Items[j].Z, m.POIT.Items[j].Nr);
                            }
                            break;
                        case "STAG":
                            {
                                m.STAG.ID = id;
                                m.STAG.NrItem = er.ReadInt16();
                                m.STAG.nrLap = er.ReadInt16();
                                int staglength = m.Header.SectionOffset[i + 1] - m.Header.SectionOffset[i] - 16;
                                m.STAG.data = er.ReadBytes(staglength);
                                ListViewItem n = new ListViewItem(1.ToString());
                                n.SubItems.Add(m.STAG.nrLap.ToString());
                                //listView4.Items.Add(n);
                                break;
                            }
                        case "KTPS":
                            m.KTPS.ID = id;
                            m.KTPS.NrItem = er.ReadInt32();
                            m.KTPS.Items = new List<MKDS_Course_Editor.nkm.KTPs>();
                            for (int j = 0; j < m.KTPS.NrItem; j++)
                            {
                                ListViewItem n = new ListViewItem((j + 1).ToString());
                                m.KTPS.Items.Add(new MKDS_Course_Editor.nkm.KTPs());
                                m.KTPS.Items[j].TransX = (float)(er.ReadInt32()) / 4096f;
                                m.KTPS.Items[j].TransY = (float)(er.ReadInt32()) / 4096f;
                                m.KTPS.Items[j].TransZ = (float)(er.ReadInt32()) / 4096f;
                                m.KTPS.Items[j].RotateX = (float)(er.ReadInt32()) / 4096f;
                                m.KTPS.Items[j].RotateY = (float)(er.ReadInt32()) / 4096f;
                                m.KTPS.Items[j].RotateZ = (float)(er.ReadInt32()) / 4096f;
                                m.KTPS.Items[j].Setting1 = er.ReadBytes(4);
                                n.SubItems.Add(m.KTPS.Items[j].TransX.ToString());
                                n.SubItems.Add(m.KTPS.Items[j].TransY.ToString());
                                n.SubItems.Add(m.KTPS.Items[j].TransZ.ToString());
                                n.SubItems.Add(m.KTPS.Items[j].RotateX.ToString());
                                n.SubItems.Add(m.KTPS.Items[j].RotateY.ToString());
                                n.SubItems.Add(m.KTPS.Items[j].RotateZ.ToString());
                                n.SubItems.Add(System.BitConverter.ToString(m.KTPS.Items[j].Setting1).Replace("-", ""));
                                //listView5.Items.Add(n);
                                //nkmEditorControl1.KTPS.Add(m.KTPS.Items[j].TransX, m.KTPS.Items[j].TransY, m.KTPS.Items[j].TransZ, m.KTPS.Items[j].RotateY, Properties.Resources.start);
                            }
                            break;
                        case "KTPJ":
                            m.KTPJ.ID = id;
                            m.KTPJ.NrItem = er.ReadInt32();
                            m.KTPJ.Items = new List<MKDS_Course_Editor.nkm.KTPj>();
                            for (int j = 0; j < m.KTPJ.NrItem; j++)
                            {
                                ListViewItem n = new ListViewItem((j + 1).ToString());
                                m.KTPJ.Items.Add(new MKDS_Course_Editor.nkm.KTPj());
                                m.KTPJ.Items[j].TransX = (float)(er.ReadInt32()) / 4096f;
                                m.KTPJ.Items[j].TransY = (float)(er.ReadInt32()) / 4096f;
                                m.KTPJ.Items[j].TransZ = (float)(er.ReadInt32()) / 4096f;
                                m.KTPJ.Items[j].RotateX = (float)(er.ReadInt32()) / 4096f;
                                m.KTPJ.Items[j].RotateY = (float)(er.ReadInt32()) / 4096f;
                                m.KTPJ.Items[j].RotateZ = (float)(er.ReadInt32()) / 4096f;
                                m.KTPJ.Items[j].Setting1 = er.ReadBytes(4);
                                if ((m.Header.SectionOffset[i + 1] - m.Header.SectionOffset[i] - 8) / 32 == m.KTPJ.NrItem)
                                {
                                    m.KTPJ.Items[j].Setting2 = er.ReadBytes(4);
                                }
                                else
                                {
                                    m.KTPJ.Items[j].Setting2 = inttobyte(j);
                                }
                                n.SubItems.Add(m.KTPJ.Items[j].TransX.ToString());
                                n.SubItems.Add(m.KTPJ.Items[j].TransY.ToString());
                                n.SubItems.Add(m.KTPJ.Items[j].TransZ.ToString());
                                n.SubItems.Add(m.KTPJ.Items[j].RotateX.ToString());
                                n.SubItems.Add(m.KTPJ.Items[j].RotateY.ToString());
                                n.SubItems.Add(m.KTPJ.Items[j].RotateZ.ToString());
                                n.SubItems.Add(System.BitConverter.ToString(m.KTPJ.Items[j].Setting1).Replace("-", ""));
                                n.SubItems.Add(System.BitConverter.ToString(m.KTPJ.Items[j].Setting2).Replace("-", ""));
                                //listView6.Items.Add(n);
                                //nkmEditorControl1.KTPJ.Add(m.KTPJ.Items[j].TransX, m.KTPJ.Items[j].TransY, m.KTPJ.Items[j].TransZ, Color.Orange);
                            }
                            break;
                        case "KTP2":
                            m.KTP2.ID = id;
                            m.KTP2.NrItem = er.ReadInt32();
                            m.KTP2.Items = new List<MKDS_Course_Editor.nkm.KTP2>();
                            for (int j = 0; j < m.KTP2.NrItem; j++)
                            {
                                ListViewItem n = new ListViewItem((j + 1).ToString());
                                m.KTP2.Items.Add(new MKDS_Course_Editor.nkm.KTP2());
                                m.KTP2.Items[j].TransX = (float)(er.ReadInt32()) / 4096f;
                                m.KTP2.Items[j].TransY = (float)(er.ReadInt32()) / 4096f;
                                m.KTP2.Items[j].TransZ = (float)(er.ReadInt32()) / 4096f;
                                m.KTP2.Items[j].RotateX = (float)(er.ReadInt32()) / 4096f;
                                m.KTP2.Items[j].RotateY = (float)(er.ReadInt32()) / 4096f;
                                m.KTP2.Items[j].RotateZ = (float)(er.ReadInt32()) / 4096f;
                                m.KTP2.Items[j].Setting1 = er.ReadBytes(4);
                                n.SubItems.Add(m.KTP2.Items[j].TransX.ToString());
                                n.SubItems.Add(m.KTP2.Items[j].TransY.ToString());
                                n.SubItems.Add(m.KTP2.Items[j].TransZ.ToString());
                                n.SubItems.Add(m.KTP2.Items[j].RotateX.ToString());
                                n.SubItems.Add(m.KTP2.Items[j].RotateY.ToString());
                                n.SubItems.Add(m.KTP2.Items[j].RotateZ.ToString());
                                n.SubItems.Add(System.BitConverter.ToString(m.KTP2.Items[j].Setting1).Replace("-", ""));
                                //listView7.Items.Add(n);
                                //nkmEditorControl1.KTP2.Add(m.KTP2.Items[j].TransX, m.KTP2.Items[j].TransY, m.KTP2.Items[j].TransZ, Color.LightSkyBlue);
                            }
                            break;
                        case "KTPC":
                            m.KTPC.ID = id;
                            m.KTPC.NrItem = er.ReadInt32();
                            m.KTPC.Items = new List<MKDS_Course_Editor.nkm.KTPc>();
                            for (int j = 0; j < m.KTPC.NrItem; j++)
                            {
                                ListViewItem n = new ListViewItem((j + 1).ToString());
                                m.KTPC.Items.Add(new MKDS_Course_Editor.nkm.KTPc());
                                m.KTPC.Items[j].TransX = (float)(er.ReadInt32()) / 4096f;
                                m.KTPC.Items[j].TransY = (float)(er.ReadInt32()) / 4096f;
                                m.KTPC.Items[j].TransZ = (float)(er.ReadInt32()) / 4096f;
                                m.KTPC.Items[j].RotateX = (float)(er.ReadInt32()) / 4096f;
                                m.KTPC.Items[j].RotateY = (float)(er.ReadInt32()) / 4096f;
                                m.KTPC.Items[j].RotateZ = (float)(er.ReadInt32()) / 4096f;
                                m.KTPC.Items[j].Setting1 = er.ReadBytes(4);
                                n.SubItems.Add(m.KTPC.Items[j].TransX.ToString());
                                n.SubItems.Add(m.KTPC.Items[j].TransY.ToString());
                                n.SubItems.Add(m.KTPC.Items[j].TransZ.ToString());
                                n.SubItems.Add(m.KTPC.Items[j].RotateX.ToString());
                                n.SubItems.Add(m.KTPC.Items[j].RotateY.ToString());
                                n.SubItems.Add(m.KTPC.Items[j].RotateZ.ToString());
                                n.SubItems.Add(System.BitConverter.ToString(m.KTPC.Items[j].Setting1).Replace("-", ""));
                                //listView8.Items.Add(n);
                                //nkmEditorControl1.KTPC.Add(m.KTPC.Items[j].TransX, m.KTPC.Items[j].TransY, m.KTPC.Items[j].TransZ, Color.DeepPink);
                            }
                            break;
                        case "KTPM":
                            m.KTPM.ID = id;
                            m.KTPM.NrItem = er.ReadInt32();
                            m.KTPM.Items = new List<MKDS_Course_Editor.nkm.KTPm>();
                            for (int j = 0; j < m.KTPM.NrItem; j++)
                            {
                                ListViewItem n = new ListViewItem((j + 1).ToString());
                                m.KTPM.Items.Add(new MKDS_Course_Editor.nkm.KTPm());
                                m.KTPM.Items[j].TransX = (float)(er.ReadInt32()) / 4096f;
                                m.KTPM.Items[j].TransY = (float)(er.ReadInt32()) / 4096f;
                                m.KTPM.Items[j].TransZ = (float)(er.ReadInt32()) / 4096f;
                                m.KTPM.Items[j].RotateX = (float)(er.ReadInt32()) / 4096f;
                                m.KTPM.Items[j].RotateY = (float)(er.ReadInt32()) / 4096f;
                                m.KTPM.Items[j].RotateZ = (float)(er.ReadInt32()) / 4096f;
                                m.KTPM.Items[j].Setting1 = er.ReadBytes(4);
                                n.SubItems.Add(m.KTPM.Items[j].TransX.ToString());
                                n.SubItems.Add(m.KTPM.Items[j].TransY.ToString());
                                n.SubItems.Add(m.KTPM.Items[j].TransZ.ToString());
                                n.SubItems.Add(m.KTPM.Items[j].RotateX.ToString());
                                n.SubItems.Add(m.KTPM.Items[j].RotateY.ToString());
                                n.SubItems.Add(m.KTPM.Items[j].RotateZ.ToString());
                                n.SubItems.Add(System.BitConverter.ToString(m.KTPM.Items[j].Setting1).Replace("-", ""));
                                //listView9.Items.Add(n);
                                //nkmEditorControl1.KTPM.Add(m.KTPM.Items[j].TransX, m.KTPM.Items[j].TransY, m.KTPM.Items[j].TransZ, Color.Purple);
                            }
                            break;
                        case "CPOI":
                            m.CPOI.ID = id;
                            m.CPOI.NrItem = er.ReadInt32();
                            m.CPOI.Items = new List<MKDS_Course_Editor.nkm.CPOi>();
                            for (int j = 0; j < m.CPOI.NrItem; j++)
                            {
                                ListViewItem n = new ListViewItem((j + 1).ToString());
                                m.CPOI.Items.Add(new MKDS_Course_Editor.nkm.CPOi());
                                m.CPOI.Items[j].X1 = (float)(er.ReadInt32()) / 4096f;
                                m.CPOI.Items[j].Y1 = (float)(er.ReadInt32()) / 4096f;
                                m.CPOI.Items[j].X2 = (float)(er.ReadInt32()) / 4096f;
                                m.CPOI.Items[j].Y2 = (float)(er.ReadInt32()) / 4096f;
                                m.CPOI.Items[j].Setting1 = er.ReadBytes(4);
                                m.CPOI.Items[j].Setting2 = er.ReadBytes(4);
                                m.CPOI.Items[j].Setting3 = er.ReadBytes(4);
                                m.CPOI.Items[j].SData = er.ReadBytes(4);
                                m.CPOI.Items[j].LRespawn = er.ReadBytes(4);
                                n.SubItems.Add(m.CPOI.Items[j].X1.ToString());
                                n.SubItems.Add(m.CPOI.Items[j].Y1.ToString());
                                n.SubItems.Add(m.CPOI.Items[j].X2.ToString());
                                n.SubItems.Add(m.CPOI.Items[j].Y2.ToString());
                                n.SubItems.Add(System.BitConverter.ToString(m.CPOI.Items[j].Setting1).Replace("-", ""));
                                n.SubItems.Add(System.BitConverter.ToString(m.CPOI.Items[j].Setting2).Replace("-", ""));
                                n.SubItems.Add(System.BitConverter.ToString(m.CPOI.Items[j].Setting3).Replace("-", ""));
                                n.SubItems.Add(System.BitConverter.ToString(m.CPOI.Items[j].SData).Replace("-", ""));
                                n.SubItems.Add(System.BitConverter.ToString(m.CPOI.Items[j].LRespawn).Replace("-", ""));
                                //listView10.Items.Add(n);
                                //nkmEditorControl1.CPOI.Add(m.CPOI.Items[j].X1, m.CPOI.Items[j].Y1, m.CPOI.Items[j].X2, m.CPOI.Items[j].Y2);
                            }
                            break;
                        case "CPAT":
                            m.CPAT.ID = id;
                            m.CPAT.NrItem = er.ReadInt32();
                            m.CPAT.Items = new List<MKDS_Course_Editor.nkm.CPAt>();
                            for (int j = 0; j < m.CPAT.NrItem; j++)
                            {
                                ListViewItem n = new ListViewItem((j + 1).ToString());
                                m.CPAT.Items.Add(new MKDS_Course_Editor.nkm.CPAt());
                                m.CPAT.Items[j].Setting1 = (float)(er.ReadInt32()) / 4096f;
                                m.CPAT.Items[j].Setting2 = er.ReadBytes(4);
                                m.CPAT.Items[j].Setting3 = er.ReadBytes(4);
                                n.SubItems.Add(m.CPAT.Items[j].Setting1.ToString());
                                n.SubItems.Add(System.BitConverter.ToString(m.CPAT.Items[j].Setting2).Replace("-", ""));
                                n.SubItems.Add(System.BitConverter.ToString(m.CPAT.Items[j].Setting3).Replace("-", ""));
                                //listView11.Items.Add(n);
                            }
                            break;
                        case "IPOI":
                            m.IPOI.ID = id;
                            m.IPOI.NrItem = er.ReadInt32();
                            m.IPOI.Items = new List<MKDS_Course_Editor.nkm.IPOi>();
                            for (int j = 0; j < m.IPOI.NrItem; j++)
                            {
                                ListViewItem n = new ListViewItem((j + 1).ToString());
                                m.IPOI.Items.Add(new MKDS_Course_Editor.nkm.IPOi());
                                m.IPOI.Items[j].X = (float)(er.ReadInt32()) / 4096f;
                                m.IPOI.Items[j].Y = (float)(er.ReadInt32()) / 4096f;
                                m.IPOI.Items[j].Z = (float)(er.ReadInt32()) / 4096f;
                                //m.IPOI.Items[j].Nr = er.ReadInt16();
                                m.IPOI.Items[j].Setting1 = er.ReadBytes(4);
                                if ((m.Header.SectionOffset[i + 1] - m.Header.SectionOffset[i] - 8) / 20 == m.IPOI.NrItem)
                                {
                                    m.IPOI.Items[j].Setting2 = er.ReadBytes(4);
                                }
                                else
                                {
                                    m.IPOI.Items[j].Setting2 = new byte[4];
                                }
                                n.SubItems.Add(m.IPOI.Items[j].X.ToString());
                                n.SubItems.Add(m.IPOI.Items[j].Y.ToString());
                                n.SubItems.Add(m.IPOI.Items[j].Z.ToString());
                                //n.SubItems.Add(m.IPOI.Items[j].Nr.ToString());
                                n.SubItems.Add(System.BitConverter.ToString(m.IPOI.Items[j].Setting1).Replace("-", ""));
                                n.SubItems.Add(System.BitConverter.ToString(m.IPOI.Items[j].Setting2).Replace("-", ""));
                                //listView12.Items.Add(n);
                                //nkmEditorControl1.IPOI.Add(m.IPOI.Items[j].X, m.IPOI.Items[j].Y, m.IPOI.Items[j].Z, 0);
                            }
                            break;
                        case "IPAT":
                            m.IPAT.ID = id;
                            m.IPAT.NrItem = er.ReadInt32();
                            m.IPAT.Items = new List<MKDS_Course_Editor.nkm.IPAt>();
                            for (int j = 0; j < m.IPAT.NrItem; j++)
                            {
                                ListViewItem n = new ListViewItem((j + 1).ToString());
                                m.IPAT.Items.Add(new MKDS_Course_Editor.nkm.IPAt());
                                m.IPAT.Items[j].Setting1 = (float)(er.ReadInt32()) / 4096f;
                                m.IPAT.Items[j].Setting2 = er.ReadBytes(4);
                                m.IPAT.Items[j].Setting3 = er.ReadBytes(4);
                                n.SubItems.Add(m.IPAT.Items[j].Setting1.ToString());
                                n.SubItems.Add(System.BitConverter.ToString(m.IPAT.Items[j].Setting2).Replace("-", ""));
                                n.SubItems.Add(System.BitConverter.ToString(m.IPAT.Items[j].Setting3).Replace("-", ""));
                                //listView13.Items.Add(n);
                            }
                            break;
                        case "EPOI":
                            m.EPOI.ID = id;
                            m.EPOI.NrItem = er.ReadInt32();
                            m.EPOI.Items = new List<MKDS_Course_Editor.nkm.EPOi>();
                            for (int j = 0; j < m.EPOI.NrItem; j++)
                            {
                                ListViewItem n = new ListViewItem((j + 1).ToString());
                                m.EPOI.Items.Add(new MKDS_Course_Editor.nkm.EPOi());
                                m.EPOI.Items[j].X = (float)(er.ReadInt32()) / 4096f;
                                m.EPOI.Items[j].Y = (float)(er.ReadInt32()) / 4096f;
                                m.EPOI.Items[j].Z = (float)(er.ReadInt32()) / 4096f;
                                m.EPOI.Items[j].Setting1 = er.ReadBytes(4);
                                m.EPOI.Items[j].Setting2 = er.ReadBytes(4);
                                m.EPOI.Items[j].Setting3 = er.ReadBytes(4);
                                n.SubItems.Add(m.EPOI.Items[j].X.ToString());
                                n.SubItems.Add(m.EPOI.Items[j].Y.ToString());
                                n.SubItems.Add(m.EPOI.Items[j].Z.ToString());
                                n.SubItems.Add(System.BitConverter.ToString(m.EPOI.Items[j].Setting1).Replace("-", ""));
                                n.SubItems.Add(System.BitConverter.ToString(m.EPOI.Items[j].Setting2).Replace("-", ""));
                                n.SubItems.Add(System.BitConverter.ToString(m.EPOI.Items[j].Setting3).Replace("-", ""));
                                //listView14.Items.Add(n);
                                //nkmEditorControl1.EPOI.Add(m.EPOI.Items[j].X, m.EPOI.Items[j].Y, m.EPOI.Items[j].Z, 0);
                            }
                            break;
                        case "EPAT":
                            m.EPAT.ID = id;
                            m.EPAT.NrItem = er.ReadInt32();
                            m.EPAT.Items = new List<MKDS_Course_Editor.nkm.EPAt>();
                            for (int j = 0; j < m.EPAT.NrItem; j++)
                            {
                                ListViewItem n = new ListViewItem((j + 1).ToString());
                                m.EPAT.Items.Add(new MKDS_Course_Editor.nkm.EPAt());
                                m.EPAT.Items[j].Setting1 = (float)(er.ReadInt32()) / 4096f;
                                m.EPAT.Items[j].Setting2 = er.ReadBytes(4);
                                m.EPAT.Items[j].Setting3 = er.ReadBytes(4);
                                n.SubItems.Add(m.EPAT.Items[j].Setting1.ToString());
                                n.SubItems.Add(System.BitConverter.ToString(m.EPAT.Items[j].Setting2).Replace("-", ""));
                                n.SubItems.Add(System.BitConverter.ToString(m.EPAT.Items[j].Setting3).Replace("-", ""));
                                //listView15.Items.Add(n);
                            }
                            break;
                        case "MEPO":
                            m.MEPO.ID = id;
                            m.MEPO.NrItem = er.ReadInt32();
                            m.MEPO.Items = new List<MKDS_Course_Editor.nkm.MEPo>();
                            for (int j = 0; j < m.MEPO.NrItem; j++)
                            {
                                ListViewItem n = new ListViewItem((j + 1).ToString());
                                m.MEPO.Items.Add(new MKDS_Course_Editor.nkm.MEPo());
                                m.MEPO.Items[j].X = (float)(er.ReadInt32()) / 4096f;
                                m.MEPO.Items[j].Y = (float)(er.ReadInt32()) / 4096f;
                                m.MEPO.Items[j].Z = (float)(er.ReadInt32()) / 4096f;
                                m.MEPO.Items[j].Setting1 = er.ReadBytes(4);
                                m.MEPO.Items[j].Setting2 = er.ReadBytes(4);
                                m.MEPO.Items[j].Setting3 = er.ReadBytes(4);
                                n.SubItems.Add(m.MEPO.Items[j].X.ToString());
                                n.SubItems.Add(m.MEPO.Items[j].Y.ToString());
                                n.SubItems.Add(m.MEPO.Items[j].Z.ToString());
                                n.SubItems.Add(System.BitConverter.ToString(m.MEPO.Items[j].Setting1).Replace("-", ""));
                                n.SubItems.Add(System.BitConverter.ToString(m.MEPO.Items[j].Setting2).Replace("-", ""));
                                n.SubItems.Add(System.BitConverter.ToString(m.MEPO.Items[j].Setting3).Replace("-", ""));
                                //listView16.Items.Add(n);
                                //nkmEditorControl1.MEPO.Add(m.MEPO.Items[j].X, m.MEPO.Items[j].Y, m.MEPO.Items[j].Z, 0);
                            }
                            break;
                        case "MEPA":
                            m.MEPA.ID = id;
                            m.MEPA.NrItem = er.ReadInt32();
                            m.MEPA.Items = new List<MKDS_Course_Editor.nkm.MEPa>();
                            for (int j = 0; j < m.MEPA.NrItem; j++)
                            {
                                ListViewItem n = new ListViewItem((j + 1).ToString());
                                m.MEPA.Items.Add(new MKDS_Course_Editor.nkm.MEPa());
                                m.MEPA.Items[j].Setting1 = (float)(er.ReadInt32()) / 4096f;
                                m.MEPA.Items[j].Setting2 = er.ReadBytes(4);
                                m.MEPA.Items[j].Setting3 = er.ReadBytes(4);
                                m.MEPA.Items[j].Setting4 = er.ReadBytes(4);
                                m.MEPA.Items[j].Setting5 = er.ReadBytes(4);
                                n.SubItems.Add(m.MEPA.Items[j].Setting1.ToString());
                                n.SubItems.Add(System.BitConverter.ToString(m.MEPA.Items[j].Setting2).Replace("-", ""));
                                n.SubItems.Add(System.BitConverter.ToString(m.MEPA.Items[j].Setting3).Replace("-", ""));
                                n.SubItems.Add(System.BitConverter.ToString(m.MEPA.Items[j].Setting4).Replace("-", ""));
                                n.SubItems.Add(System.BitConverter.ToString(m.MEPA.Items[j].Setting5).Replace("-", ""));
                                //listView17.Items.Add(n);
                            }
                            break;
                        case "AREA":
                            m.AREA.ID = id;
                            m.AREA.NrItem = er.ReadInt32();
                            m.AREA.Items = new List<MKDS_Course_Editor.nkm.AREa>();
                            for (int j = 0; j < m.AREA.NrItem; j++)
                            {
                                ListViewItem n = new ListViewItem((j + 1).ToString());
                                m.AREA.Items.Add(new MKDS_Course_Editor.nkm.AREa());
                                m.AREA.Items[j].TransX = (float)(er.ReadInt32()) / 4096f;
                                m.AREA.Items[j].TransY = (float)(er.ReadInt32()) / 4096f;
                                m.AREA.Items[j].TransZ = (float)(er.ReadInt32()) / 4096f;
                                m.AREA.Items[j].Setting1 = (float)(er.ReadInt32()) / 4096f;
                                m.AREA.Items[j].Setting2 = (float)(er.ReadInt32()) / 4096f;
                                m.AREA.Items[j].Setting3 = (float)(er.ReadInt32()) / 4096f;
                                m.AREA.Items[j].Setting4 = er.ReadBytes(4);
                                m.AREA.Items[j].Setting5 = er.ReadBytes(4);
                                m.AREA.Items[j].Setting6 = er.ReadBytes(4);
                                m.AREA.Items[j].Setting7 = er.ReadBytes(4);
                                m.AREA.Items[j].Setting8 = er.ReadBytes(4);
                                m.AREA.Items[j].Setting9 = er.ReadBytes(4);
                                m.AREA.Items[j].Setting10 = er.ReadBytes(4);
                                m.AREA.Items[j].Setting11 = er.ReadBytes(4);
                                m.AREA.Items[j].Setting12 = er.ReadBytes(4);
                                m.AREA.Items[j].Setting13 = er.ReadBytes(4);
                                m.AREA.Items[j].Setting14 = er.ReadBytes(4);
                                m.AREA.Items[j].Setting15 = er.ReadBytes(4);
                                n.SubItems.Add(m.AREA.Items[j].TransX.ToString());
                                n.SubItems.Add(m.AREA.Items[j].TransY.ToString());
                                n.SubItems.Add(m.AREA.Items[j].TransZ.ToString());
                                n.SubItems.Add(m.AREA.Items[j].Setting1.ToString());
                                n.SubItems.Add(m.AREA.Items[j].Setting2.ToString());
                                n.SubItems.Add(m.AREA.Items[j].Setting3.ToString());
                                n.SubItems.Add(System.BitConverter.ToString(m.AREA.Items[j].Setting4).Replace("-", ""));
                                n.SubItems.Add(System.BitConverter.ToString(m.AREA.Items[j].Setting5).Replace("-", ""));
                                n.SubItems.Add(System.BitConverter.ToString(m.AREA.Items[j].Setting6).Replace("-", ""));
                                n.SubItems.Add(System.BitConverter.ToString(m.AREA.Items[j].Setting7).Replace("-", ""));
                                n.SubItems.Add(System.BitConverter.ToString(m.AREA.Items[j].Setting8).Replace("-", ""));
                                n.SubItems.Add(System.BitConverter.ToString(m.AREA.Items[j].Setting9).Replace("-", ""));
                                n.SubItems.Add(System.BitConverter.ToString(m.AREA.Items[j].Setting10).Replace("-", ""));
                                n.SubItems.Add(System.BitConverter.ToString(m.AREA.Items[j].Setting11).Replace("-", ""));
                                n.SubItems.Add(System.BitConverter.ToString(m.AREA.Items[j].Setting12).Replace("-", ""));
                                n.SubItems.Add(System.BitConverter.ToString(m.AREA.Items[j].Setting13).Replace("-", ""));
                                n.SubItems.Add(System.BitConverter.ToString(m.AREA.Items[j].Setting14).Replace("-", ""));
                                n.SubItems.Add(System.BitConverter.ToString(m.AREA.Items[j].Setting15).Replace("-", ""));
                                //listView18.Items.Add(n);
                                //nkmEditorControl1.AREA.Add(m.AREA.Items[j].TransX, m.AREA.Items[j].TransY, m.AREA.Items[j].TransZ, 0);
                            }
                            break;
                        case "CAME":
                            m.CAME.ID = id;
                            m.CAME.NrItem = er.ReadInt32();
                            m.CAME.Items = new List<MKDS_Course_Editor.nkm.CAMe>();
                            for (int j = 0; j < m.CAME.NrItem; j++)
                            {
                                ListViewItem n = new ListViewItem((j + 1).ToString());
                                m.CAME.Items.Add(new MKDS_Course_Editor.nkm.CAMe());
                                m.CAME.Items[j].X1 = (float)(er.ReadInt32()) / 4096f;
                                m.CAME.Items[j].Y1 = (float)(er.ReadInt32()) / 4096f;
                                m.CAME.Items[j].Z1 = (float)(er.ReadInt32()) / 4096f;
                                m.CAME.Items[j].Zero1 = (float)(er.ReadInt32()) / 4096f;
                                m.CAME.Items[j].Zero1 = (float)(er.ReadInt32()) / 4096f;
                                m.CAME.Items[j].Zero1 = (float)(er.ReadInt32()) / 4096f;
                                m.CAME.Items[j].X2 = (float)(er.ReadInt32()) / 4096f;
                                m.CAME.Items[j].Y2 = (float)(er.ReadInt32()) / 4096f;
                                m.CAME.Items[j].Z2 = (float)(er.ReadInt32()) / 4096f;
                                m.CAME.Items[j].X3 = (float)(er.ReadInt32()) / 4096f;
                                m.CAME.Items[j].Y3 = (float)(er.ReadInt32()) / 4096f;
                                m.CAME.Items[j].Z3 = (float)(er.ReadInt32()) / 4096f;
                                m.CAME.Items[j].Setting1 = er.ReadBytes(4);
                                m.CAME.Items[j].Setting2 = er.ReadBytes(4);
                                m.CAME.Items[j].Setting3 = er.ReadBytes(4);
                                m.CAME.Items[j].Setting4 = er.ReadBytes(4);
                                m.CAME.Items[j].Setting5 = er.ReadBytes(4);
                                m.CAME.Items[j].Setting6 = er.ReadBytes(4);
                                m.CAME.Items[j].Setting7 = er.ReadBytes(4);
                                n.SubItems.Add(m.CAME.Items[j].X1.ToString());
                                n.SubItems.Add(m.CAME.Items[j].Y1.ToString());
                                n.SubItems.Add(m.CAME.Items[j].Z1.ToString());
                                n.SubItems.Add(m.CAME.Items[j].Zero1.ToString());
                                n.SubItems.Add(m.CAME.Items[j].Zero1.ToString());
                                n.SubItems.Add(m.CAME.Items[j].Zero1.ToString());
                                n.SubItems.Add(m.CAME.Items[j].X2.ToString());
                                n.SubItems.Add(m.CAME.Items[j].Y2.ToString());
                                n.SubItems.Add(m.CAME.Items[j].Z2.ToString());
                                n.SubItems.Add(m.CAME.Items[j].X3.ToString());
                                n.SubItems.Add(m.CAME.Items[j].Y3.ToString());
                                n.SubItems.Add(m.CAME.Items[j].Z3.ToString());
                                n.SubItems.Add(System.BitConverter.ToString(m.CAME.Items[j].Setting1).Replace("-", ""));
                                n.SubItems.Add(System.BitConverter.ToString(m.CAME.Items[j].Setting2).Replace("-", ""));
                                n.SubItems.Add(System.BitConverter.ToString(m.CAME.Items[j].Setting3).Replace("-", ""));
                                n.SubItems.Add(System.BitConverter.ToString(m.CAME.Items[j].Setting4).Replace("-", ""));
                                n.SubItems.Add(System.BitConverter.ToString(m.CAME.Items[j].Setting5).Replace("-", ""));
                                n.SubItems.Add(System.BitConverter.ToString(m.CAME.Items[j].Setting6).Replace("-", ""));
                                n.SubItems.Add(System.BitConverter.ToString(m.CAME.Items[j].Setting7).Replace("-", ""));
                                //listView19.Items.Add(n);
                                //nkmEditorControl1.CAME.Add(m.CAME.Items[j].X1, m.CAME.Items[j].Y1, m.CAME.Items[j].Z1, m.CAME.Items[j].X2, m.CAME.Items[j].Y2, m.CAME.Items[j].Z2, m.CAME.Items[j].X3, m.CAME.Items[j].Y3, m.CAME.Items[j].Z3);
                            }
                            break;
                        default:
                            MessageBox.Show("error");
                            er.Close();
                            this.Close();
                            return;
                            break;
                    }
                }
                er.Close();
            }
            else
            {

            }
        }
        void ParseNKM()
        {
            int mat = 0;
            foreach (Nsbmd n in nsbm)
            {
                mat += n.models[0].Materials.Count;
            }
            for (int i = 0; i < m.OBJI.Items.Count; i++)
            {
                float scale = getScale2(BitConverter.ToString(m.OBJI.Items[i].ObjectID).Replace("-", ""));
                switch (BitConverter.ToString(m.OBJI.Items[i].ObjectID).Replace("-", ""))
                {
                    case "0100":
                        //mat = AddModel(path + "\\" + "unpack\\" + "MapObj\\beach_waterA.nsbmd", new Vector3(m.OBJI.Items[i].TransX, m.OBJI.Items[i].TransY, m.OBJI.Items[i].TransZ), mat, new Vector3(m.OBJI.Items[i].RotX, m.OBJI.Items[i].RotY, m.OBJI.Items[i].RotZ), new Vector3(m.OBJI.Items[i].ScaleX / scale, m.OBJI.Items[i].ScaleY / scale, m.OBJI.Items[i].ScaleZ / scale), m.OBJI.Items[i].Setting5[0], m.OBJI.Items[i]);
                        //mat = AddModel(path + "\\" + "unpack\\" + "MapObj\\beach_waterC.nsbmd", new Vector3(m.OBJI.Items[i].TransX, m.OBJI.Items[i].TransY, m.OBJI.Items[i].TransZ), mat, new Vector3(m.OBJI.Items[i].RotX, m.OBJI.Items[i].RotY, m.OBJI.Items[i].RotZ), new Vector3(m.OBJI.Items[i].ScaleX / scale, m.OBJI.Items[i].ScaleY / scale, m.OBJI.Items[i].ScaleZ / scale), m.OBJI.Items[i].Setting5[0], m.OBJI.Items[i]);
                        break;
                    case "0300":
                        mat = AddModel(path + "\\" + "unpack\\" + "MapObj\\town_waterA.nsbmd", new Vector3(m.OBJI.Items[i].TransX, m.OBJI.Items[i].TransY, m.OBJI.Items[i].TransZ), mat, new Vector3(m.OBJI.Items[i].RotX, m.OBJI.Items[i].RotY, m.OBJI.Items[i].RotZ), new Vector3(m.OBJI.Items[i].ScaleX / scale, m.OBJI.Items[i].ScaleY / scale, m.OBJI.Items[i].ScaleZ / scale), m.OBJI.Items[i].Setting5[0], m.OBJI.Items[i]);
                        mat = AddModel(path + "\\" + "unpack\\" + "MapObj\\town_waterC.nsbmd", new Vector3(m.OBJI.Items[i].TransX, m.OBJI.Items[i].TransY, m.OBJI.Items[i].TransZ), mat, new Vector3(m.OBJI.Items[i].RotX, m.OBJI.Items[i].RotY, m.OBJI.Items[i].RotZ), new Vector3(m.OBJI.Items[i].ScaleX / scale, m.OBJI.Items[i].ScaleY / scale, m.OBJI.Items[i].ScaleZ / scale), m.OBJI.Items[i].Setting5[0], m.OBJI.Items[i]);
                        break;
                    case "6500":
                        mat = AddModel(path3 + "\\" + "unpack\\" + "itembox.nsbmd", new Vector3(m.OBJI.Items[i].TransX, m.OBJI.Items[i].TransY, m.OBJI.Items[i].TransZ), mat, new Vector3(m.OBJI.Items[i].RotX, m.OBJI.Items[i].RotY, m.OBJI.Items[i].RotZ), new Vector3(m.OBJI.Items[i].ScaleX / scale, m.OBJI.Items[i].ScaleY / scale, m.OBJI.Items[i].ScaleZ / scale), m.OBJI.Items[i].Setting5[0], m.OBJI.Items[i]);
                        break;
                    case "CA00":
                        mat = AddModel(path + "\\" + "unpack\\" + "MapObj\\koopa_block.nsbmd", new Vector3(m.OBJI.Items[i].TransX, m.OBJI.Items[i].TransY, m.OBJI.Items[i].TransZ), mat, new Vector3(m.OBJI.Items[i].RotX, m.OBJI.Items[i].RotY, m.OBJI.Items[i].RotZ), new Vector3(m.OBJI.Items[i].ScaleX / scale, m.OBJI.Items[i].ScaleY / scale, m.OBJI.Items[i].ScaleZ / scale), m.OBJI.Items[i].Setting5[0], m.OBJI.Items[i]);
                        break;
                    case "CB00":
                        mat = AddModel(path + "\\" + "unpack\\" + "MapObj\\gear_black.nsbmd", new Vector3(m.OBJI.Items[i].TransX, m.OBJI.Items[i].TransY, m.OBJI.Items[i].TransZ), mat, new Vector3(m.OBJI.Items[i].RotX, m.OBJI.Items[i].RotY, m.OBJI.Items[i].RotZ), new Vector3(m.OBJI.Items[i].ScaleX / scale, m.OBJI.Items[i].ScaleY / scale, m.OBJI.Items[i].ScaleZ / scale), m.OBJI.Items[i].Setting5[0], m.OBJI.Items[i]);
                        break;
                    case "CD00":
                        mat = AddModel(path + "\\" + "unpack\\" + "MapObj\\second_hand.nsbmd", new Vector3(m.OBJI.Items[i].TransX, m.OBJI.Items[i].TransY, m.OBJI.Items[i].TransZ), mat, new Vector3(m.OBJI.Items[i].RotX, m.OBJI.Items[i].RotY, m.OBJI.Items[i].RotZ), new Vector3(m.OBJI.Items[i].ScaleX / scale, m.OBJI.Items[i].ScaleY / scale, m.OBJI.Items[i].ScaleZ / scale), m.OBJI.Items[i].Setting5[0], m.OBJI.Items[i]);
                        break;
                    case "CE00":
                        mat = AddModel(path + "\\" + "unpack\\" + "MapObj\\test_cylinder.nsbmd", new Vector3(m.OBJI.Items[i].TransX, m.OBJI.Items[i].TransY, m.OBJI.Items[i].TransZ), mat, new Vector3(m.OBJI.Items[i].RotX, m.OBJI.Items[i].RotY, m.OBJI.Items[i].RotZ), new Vector3(m.OBJI.Items[i].ScaleX / scale, m.OBJI.Items[i].ScaleY / scale, m.OBJI.Items[i].ScaleZ / scale), m.OBJI.Items[i].Setting5[0], m.OBJI.Items[i]);
                        break;
                    case "CF00":
                        mat = AddModel(path + "\\" + "unpack\\" + "MapObj\\pendulum.nsbmd", new Vector3(m.OBJI.Items[i].TransX, m.OBJI.Items[i].TransY, m.OBJI.Items[i].TransZ), mat, new Vector3(m.OBJI.Items[i].RotX, m.OBJI.Items[i].RotY, m.OBJI.Items[i].RotZ), new Vector3(m.OBJI.Items[i].ScaleX / scale, m.OBJI.Items[i].ScaleY / scale, m.OBJI.Items[i].ScaleZ / scale), m.OBJI.Items[i].Setting5[0], m.OBJI.Items[i]);
                        break;
                    case "D000":
                        mat = AddModel(path + "\\" + "unpack\\" + "MapObj\\rotary_room.nsbmd", new Vector3(m.OBJI.Items[i].TransX, m.OBJI.Items[i].TransY, m.OBJI.Items[i].TransZ), mat, new Vector3(m.OBJI.Items[i].RotX, m.OBJI.Items[i].RotY, m.OBJI.Items[i].RotZ), new Vector3(m.OBJI.Items[i].ScaleX / scale, m.OBJI.Items[i].ScaleY / scale, m.OBJI.Items[i].ScaleZ / scale), m.OBJI.Items[i].Setting5[0], m.OBJI.Items[i]);
                        break;
                    case "D100":
                        mat = AddModel(path + "\\" + "unpack\\" + "MapObj\\rotary_bridge.nsbmd", new Vector3(m.OBJI.Items[i].TransX, m.OBJI.Items[i].TransY, m.OBJI.Items[i].TransZ), mat, new Vector3(m.OBJI.Items[i].RotX + 90, m.OBJI.Items[i].RotY + 90, m.OBJI.Items[i].RotZ + 90), new Vector3(m.OBJI.Items[i].ScaleX / scale, m.OBJI.Items[i].ScaleY / scale, m.OBJI.Items[i].ScaleZ / scale), m.OBJI.Items[i].Setting5[0], m.OBJI.Items[i]);
                        break;
                    case "2E01":
                        mat = AddModel(path + "\\" + "unpack\\" + "MapObj\\BeachTree1.nsbmd", new Vector3(m.OBJI.Items[i].TransX, m.OBJI.Items[i].TransY, m.OBJI.Items[i].TransZ), mat, new Vector3(m.OBJI.Items[i].RotX, m.OBJI.Items[i].RotY, m.OBJI.Items[i].RotZ), new Vector3(m.OBJI.Items[i].ScaleX / scale, m.OBJI.Items[i].ScaleY / scale, m.OBJI.Items[i].ScaleZ / scale), m.OBJI.Items[i].Setting5[0], m.OBJI.Items[i]);
                        break;
                    case "3A01":
                        mat = AddModel(path + "\\" + "unpack\\" + "MapObj\\CrossTree1.nsbmd", new Vector3(m.OBJI.Items[i].TransX, m.OBJI.Items[i].TransY, m.OBJI.Items[i].TransZ), mat, new Vector3(m.OBJI.Items[i].RotX, m.OBJI.Items[i].RotY, m.OBJI.Items[i].RotZ), new Vector3(m.OBJI.Items[i].ScaleX / scale, m.OBJI.Items[i].ScaleY / scale, m.OBJI.Items[i].ScaleZ / scale), m.OBJI.Items[i].Setting5[0], m.OBJI.Items[i]);
                        break;
                    case "9601":
                        mat = AddModel(path + "\\" + "unpack\\" + "MapObj\\wanwan.nsbmd", new Vector3(m.OBJI.Items[i].TransX, m.OBJI.Items[i].TransY, m.OBJI.Items[i].TransZ), mat, new Vector3(m.OBJI.Items[i].RotX, m.OBJI.Items[i].RotY, m.OBJI.Items[i].RotZ), new Vector3(m.OBJI.Items[i].ScaleX / scale, m.OBJI.Items[i].ScaleY / scale, m.OBJI.Items[i].ScaleZ / scale), m.OBJI.Items[i].Setting5[0], m.OBJI.Items[i]);
                        break;
                    case "9B01":
                        mat = AddModel(path + "\\" + "unpack\\" + "MapObj\\PukuPuku.nsbmd", new Vector3(m.OBJI.Items[i].TransX, m.OBJI.Items[i].TransY, m.OBJI.Items[i].TransZ), mat, new Vector3(m.OBJI.Items[i].RotX, m.OBJI.Items[i].RotY, m.OBJI.Items[i].RotZ), new Vector3(m.OBJI.Items[i].ScaleX / scale, m.OBJI.Items[i].ScaleY / scale, m.OBJI.Items[i].ScaleZ / scale), m.OBJI.Items[i].Setting5[0], m.OBJI.Items[i]);
                        break;
                    case "A501":
                        mat = AddModel(path + "\\" + "unpack\\" + "MapObj\\wanwan.nsbmd", new Vector3(m.OBJI.Items[i].TransX, m.OBJI.Items[i].TransY, m.OBJI.Items[i].TransZ), mat, new Vector3(m.OBJI.Items[i].RotX, m.OBJI.Items[i].RotY, m.OBJI.Items[i].RotZ), new Vector3(m.OBJI.Items[i].ScaleX / scale, m.OBJI.Items[i].ScaleY / scale, m.OBJI.Items[i].ScaleZ / scale), m.OBJI.Items[i].Setting5[0], m.OBJI.Items[i]);
                        break;
                    case "A601":
                        mat = AddModel(path + "\\" + "unpack\\" + "MapObj\\ob_pakkun_sf.nsbmd", new Vector3(m.OBJI.Items[i].TransX, m.OBJI.Items[i].TransY, m.OBJI.Items[i].TransZ), mat, new Vector3(m.OBJI.Items[i].RotX, m.OBJI.Items[i].RotY, m.OBJI.Items[i].RotZ), new Vector3(m.OBJI.Items[i].ScaleX / scale, m.OBJI.Items[i].ScaleY / scale, m.OBJI.Items[i].ScaleZ / scale), m.OBJI.Items[i].Setting5[0], m.OBJI.Items[i]);
                        break;
                    default:
                        break;
                }
            }
        }
        int AddModel(string pathh, Vector3 posi, int mats, Vector3 ro, Vector3 sc, int ti, object ob)
        {
            nsbm.Add(NsbmdLoader.LoadNsbmd(File.OpenRead(pathh)));
            nsbm[nsbm.Count - 1].MatchTextures();
            t.Add(new NsbmdGlRenderer(mats));
            mats += nsbm[nsbm.Count - 1].models[0].Materials.Count;
            pos.Add(posi);
            scale.Add(sc);
            rot.Add(ro);
            obj.Add(ob);
            tt.Add(ti == 1);
            if (File.Exists(pathh.Replace(".nsbmd", ".nsbta")))
            {
                ani.Add(NSBTA.NSBTA.Read(pathh.Replace(".nsbmd", ".nsbta")));
                aniframeS.Add(new int[ani[ani.Count - 1].MAT.num_objs]);
                aniframeT.Add(new int[ani[ani.Count - 1].MAT.num_objs]);
                aniframeR.Add(new int[ani[ani.Count - 1].MAT.num_objs]);
                aniScaleS.Add(new int[ani[ani.Count - 1].MAT.num_objs]);
                aniScaleT.Add(new int[ani[ani.Count - 1].MAT.num_objs]);
                //timer1.Enabled = true;
            }
            else
            {
                ani.Add(new NSBTA.NSBTA.NSBTA_File());
                aniframeS.Add(new int[0]);
                aniframeT.Add(new int[0]);
                aniframeR.Add(new int[0]);
                aniScaleS.Add(new int[0]);
                aniScaleT.Add(new int[0]);
            }
            if(ob is nkm.OBJi)
            {
                treeView1.Nodes.Add(BitConverter.ToString((ob as nkm.OBJi).ObjectID).Replace("-", ""));
            }
            //Gl.glNewList(nsbm.Count - 1, Gl.GL_COMPILE);
            //Gl.glTranslatef(pos[nsbm.Count - 1].X / getScale(), pos[nsbm.Count - 1].Y / getScale(), pos[nsbm.Count - 1].Z / getScale());
            //Gl.glRotatef(rot[nsbm.Count - 1].X, 1, 0, 0);
            //Gl.glRotatef(rot[nsbm.Count - 1].Y, 0, 1, 0);
            //Gl.glRotatef(rot[nsbm.Count - 1].Z, 0, 0, 1);
            //Gl.glScalef(scale[nsbm.Count - 1].X, scale[nsbm.Count - 1].Y, scale[nsbm.Count - 1].Z);
            //t[nsbm.Count - 1].Model = nsbm[nsbm.Count - 1].models[0];
            //t[nsbm.Count - 1].RenderModel("", ani[nsbm.Count - 1], aniframeS[nsbm.Count - 1], aniframeT[nsbm.Count - 1], aniScaleS[nsbm.Count - 1], aniScaleT[nsbm.Count - 1], aniframeR[nsbm.Count - 1], new NSBCA.NSBCA.NSBCA_File(), false, -1, X, Y, dist, elev, ang);
            //Gl.glEndList();
            return mats;
        }
        private Vector3 Get3DCoords(Point coords2d, float depth)
        {
            Vector3 ret = new Vector3(X, Y, -dist);

            ret.X -= (depth * (float)Math.Cos(elev) * (float)Math.Cos(ang));
            ret.Y -= (depth * (float)Math.Sin(ang));
            ret.Z -= (depth * (float)Math.Sin(elev) * (float)Math.Cos(ang));

            float m_AspectRatio = (float)simpleOpenGlControl1.Width / (float)simpleOpenGlControl1.Height;
            float m_PixelFactorX = ((2.0f * (float)Math.Tan((35.0f * Math.PI) / 180.0f) * m_AspectRatio) / (float)(simpleOpenGlControl1.Width));
            float m_PixelFactorY = ((2.0f * (float)Math.Tan((35.0f * Math.PI) / 180.0f)) / (float)(simpleOpenGlControl1.Height));

            float x = (coords2d.X - (simpleOpenGlControl1.Width / 2f)) * m_PixelFactorX * depth;
            float y = -(coords2d.Y - (simpleOpenGlControl1.Height / 2f)) * m_PixelFactorY * depth;

            ret.X += (x * (float)Math.Sin(elev)) - (y * (float)Math.Sin(ang) * (float)Math.Cos(elev));
            ret.Y += y * (float)Math.Cos(ang);
            ret.Z -= (x * (float)Math.Cos(elev)) + (y * (float)Math.Sin(ang) * (float)Math.Sin(elev));

            return ret;
        }

        public static float X = 0.0f;
        public static float Y = 0.0f;
        public static float ang = 0.0f;
        public static float dist = 0.0f;//2.5
        public static float elev = 0.0f;
        bool wrote = false;
        private void RenderFunc1()
        {
            //update();
            RenderFunc1(false);
        }
        public float getScale()
        {
            float scal = (nsbm[0].models[0].boundScale * 4f);
            switch (course_id)
            {
                case 1:
                    scal = 1024f;
                    break;
                case 3:
                    scal = 1024f;
                    break;
                case 4:
                    scal = 512f;
                    break;
                case 5:
                    scal = 1024f;
                    break;
                case 9:
                    scal = 2048f;
                    break;
                case 10:
                    scal = 1024f;
                    break;
                case 13:
                    scal = 1024f;
                    break;
                case 35:
                    scal = 1024f;
                    break;
                case 39:
                    scal = 1024f;
                    break;
                case 42:
                    scal = 2048f;
                    break;
                case 52:
                    scal = 4096f;
                    break;
            }
            return scal;
        }
        public float getScale2(string objid)
        {
            float scal = 1;
            switch (objid)
            {
                case "0100":
                    scal = 1f;
                    break;
                case "0300":
                    scal = 1f;
                    break;
                case "6500":
                    scal = 32f;
                    break;
                case "CA00":
                    scal = 16f;
                    break;
                case "CB00":
                    scal = 8f;
                    break;
                case "CD00":
                    scal = 4f;
                    break;
                case "CE00":
                    scal = 4f;
                    break;
                case "CF00":
                    scal = 4f;
                    break;
                case "D000":
                    scal = 16f;
                    break;
                case "D100":
                    scal = 2f;
                    break;
                case "2E01":
                    scal = 64f;
                    break;
                case "3A01":
                    scal = 16f;
                    break;
                case "9601":
                    scal = 64;
                    break;
                case "9B01":
                    scal = 16f;
                    break;
                case "A501":
                    scal = 64f;
                    break;
                case "A601":
                    scal = 32f;
                    break;
            }
            return scal;
        }
        bool tex = false;
        //bool draw = false;
        private void RenderFunc1(bool anim)
        {
			Gl.glViewport(0, 0, simpleOpenGlControl1.Width, simpleOpenGlControl1.Height);
			float aspect = (float)simpleOpenGlControl1.Width / (float)simpleOpenGlControl1.Height;//(vp[2] - vp[0]) / (vp[3] - vp[1]);
			Gl.glMatrixMode(Gl.GL_PROJECTION);
			Gl.glLoadIdentity();
			Glu.gluPerspective(90, aspect, 0.02f, 1000.0f);//0.02f, 32.0f);
			Gl.glTranslatef(X, Y, -dist);
			Gl.glRotatef(elev, 1, 0, 0);
			Gl.glRotatef(ang, 0, 1, 0);
			Gl.glMatrixMode(Gl.GL_MODELVIEW);
			Gl.glLoadIdentity();
			Gl.glTranslatef(X, Y, -dist);
			Gl.glRotatef(elev, 1, 0, 0);
			Gl.glRotatef(-ang, 0, 1, 0);
			Gl.glLightfv(Gl.GL_LIGHT0, Gl.GL_POSITION, new float[] { 1, 1, 1, 0 });
			Gl.glLightfv(Gl.GL_LIGHT1, Gl.GL_POSITION, new float[] { 1, 1, 1, 0 });
			Gl.glLightfv(Gl.GL_LIGHT2, Gl.GL_POSITION, new float[] { 1, 1, 1, 0 });
			Gl.glLightfv(Gl.GL_LIGHT3, Gl.GL_POSITION, new float[] { 1, 1, 1, 0 });
			Gl.glLoadIdentity();
			Gl.glBindTexture(Gl.GL_TEXTURE_2D, 0);
			Gl.glColor3f(1.0f, 1.0f, 1.0f);
			Gl.glDepthMask(Gl.GL_TRUE);
			Gl.glClear(Gl.GL_COLOR_BUFFER_BIT | Gl.GL_DEPTH_BUFFER_BIT);
            for (int i = 0; i < nsbm.Count; i++)
            {
                try
                {
                    var mod = nsbm[i].models[0];
                    if (mod == null)
                        return;
                    if (toolStripButton1.Checked && !tt[i]) continue;
                    //if (ani[i].Header.file_size == 0 && anim) continue;
                    if (!tex)
                    {
                        t[i].Model = nsbm[i].models[0];
                    }
                    Gl.glMatrixMode(Gl.GL_MODELVIEW);
                    Gl.glLoadIdentity();
                    Gl.glPushMatrix();
                    float scal = getScale();
                    Gl.glTranslatef(pos[i].X / scal, pos[i].Y / scal, pos[i].Z / scal);
                    Gl.glRotatef(rot[i].X, 1, 0, 0);
                    Gl.glRotatef(rot[i].Y, 0, 1, 0);
                    Gl.glRotatef(rot[i].Z, 0, 0, 1);
                    if (!(i < (File.Exists(path + "\\" + "unpack\\" + "course_model_V.nsbmd") ? 1 : 2)))
                    {
                        Gl.glScalef(scale[i].X, scale[i].Y, scale[i].Z);
                    }
                    //Gl.glPushName(i); 
                    if (i == selected)
                    {
                        t[i].RenderModel("", ani[i], aniframeS[i], aniframeT[i], aniScaleS[i], aniScaleT[i], aniframeR[i], new NSBCA.NSBCA.NSBCA_File(), anim, -1, X, Y, dist, elev, ang, new NSBTP.NSBTP.NSBTP_File(), nsbm[i]);
                        /*Gl.glColor4f(255f / 255f, 255f / 255f, 128f / 255f, 127f / 255f);
                        Gl.glTranslatef(pos[i].X / scal, pos[i].Y / scal, pos[i].Z / scal);
                        Gl.glRotatef(rot[i].X, 1, 0, 0);
                        Gl.glRotatef(rot[i].Y, 0, 1, 0);
                        Gl.glRotatef(rot[i].Z, 0, 0, 1);
                        if (!(i < (File.Exists(path + "\\" + "unpack\\" + "course_model_V.nsbmd") ? 1 : 2)))
                        {
                            Gl.glScalef(scale[i].X, scale[i].Y, scale[i].Z);
                        }
                        t[i].RenderModel(elev, ang);*/
                        /*
                        Gl.glBlendColor(255f / 255f, 255f / 255f, 128f / 255f, 127f / 255f);
                        Gl.glBlendFunc(Gl.GL_ZERO, Gl.GL_SRC_COLOR); 
                       if (!wire)
                        {
                            Gl.glPolygonMode(Gl.GL_FRONT_AND_BACK, Gl.GL_FILL);
                        }
                        Gl.glLineWidth(1);
                        Gl.glTranslatef(pos[i].X / scal, pos[i].Y / scal, pos[i].Z / scal);
                        Gl.glRotatef(rot[i].X, 1, 0, 0);
                        Gl.glRotatef(rot[i].Y, 0, 1, 0);
                        Gl.glRotatef(rot[i].Z, 0, 0, 1);
                        if (!(i < (File.Exists(path + "\\" + "unpack\\" + "course_model_V.nsbmd") ? 1 : 2)))
                        {
                            Gl.glScalef(scale[i].X, scale[i].Y, scale[i].Z);
                        }
                        t[i].RenderModel("", ani[i], aniframeS[i], aniframeT[i], aniScaleS[i], aniScaleT[i], aniframeR[i], new NSBCA.NSBCA.NSBCA_File(), anim, -1, X, Y, dist, elev, ang);*/
                    }
                    else
                    {
                        t[i].RenderModel("", ani[i], aniframeS[i], aniframeT[i], aniScaleS[i], aniScaleT[i], aniframeR[i], new NSBCA.NSBCA.NSBCA_File(), anim, -1, X, Y, dist, elev, ang, new NSBTP.NSBTP.NSBTP_File(), nsbm[i]);
                    }
                    //Gl.glPopName(); 
                    //Gl.glCallList(i);
                    Gl.glPopMatrix();
                    // t2.Model = nsbm2.models[0];
                    //t2.RenderModel(file_2, ani2, aniframeS2, aniframeT2, aniScaleS2, aniScaleT2, ca);
                    if (wrote == false)
                    {
                        //renderer.WriteOBJ(null, "C:\\model\\test.obj");
                        wrote = true;
                    }
                }
                catch (Exception ex)
                {
                }
            }
            tex = true;
            //Gl.glMatrixMode(Gl.GL_TEXTURE);
            //Gl.glRotatef(45, 1, 0, 0);
        }
        private uint[] m_PickingFrameBuffer = new uint[3]; 
        // <summary> 
        /// Greatest Common Denominator (GCD). Euclidean algorithm.  
        /// </summary> 
        /// <param name="a">Width.</param> 
        /// <param name="b">Height.</param> 
        /// <returns>GCD.</returns> 
        static int GCD(int a, int b)
        {
            return b == 0 ? a : GCD(b, a % b);
        }

        public bool wire = false;
        public int fix = 0;
        protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
        {
            switch (keyData)
            {
                case Keys.Right:
                    ang += 1;
                    simpleOpenGlControl1.Refresh();
                    //RenderFunc1();
                    return true;
                case Keys.Left:
                    ang -= 1;
                    simpleOpenGlControl1.Refresh();
                    //RenderFunc1();
                    return true;
                case Keys.Up:
                    elev += 1;
                    simpleOpenGlControl1.Refresh();
                    //RenderFunc1();
                    return true;
                case Keys.Down:
                    elev -= 1;
                    simpleOpenGlControl1.Refresh();
                    //RenderFunc1();
                    return true;
                case Keys.Z:
                    X -= 0.05f;
                    simpleOpenGlControl1.Refresh();
                    //RenderFunc1();
                    return true;
                case Keys.X:
                    X += 0.05f;
                    simpleOpenGlControl1.Refresh();
                    //RenderFunc1();
                    return true;
                case Keys.A:
                    Y -= 0.05f;
                    simpleOpenGlControl1.Refresh();
                    //RenderFunc1();
                    return true;
                case Keys.S:
                    Y += 0.05f;
                    simpleOpenGlControl1.Refresh();
                    //RenderFunc1();
                    return true;
                case Keys.W:
                    if (wire == false)
                    {
                        Gl.glPolygonMode(Gl.GL_FRONT_AND_BACK, Gl.GL_LINE);
                        simpleOpenGlControl1.Refresh();
                        //RenderFunc1();
                        wire = true;
                    }
                    else
                    {
                        Gl.glPolygonMode(Gl.GL_FRONT_AND_BACK, Gl.GL_FILL);
                        simpleOpenGlControl1.Refresh();
                        //RenderFunc1();
                        wire = false;
                    }
                    return true;
                case Keys.Escape:
                    X = 0.0f;
                    Y = 0.0f;
                    ang = 0.0f;
                    dist = 0.0f;
                    elev = 0.0f;
                    simpleOpenGlControl1.Refresh();
                    //RenderFunc1();
                    return true;
                case Keys.T:
                    elev = 90;
                    ang = 0;
                    simpleOpenGlControl1.Refresh();
                    //RenderFunc1();
                    return true;
                default:
                    return base.ProcessCmdKey(ref msg, keyData);
            }
        }

        private void simpleOpenGlControl1_Resize(object sender, EventArgs e)
        {
            /*simpleOpenGlControl1.MakeCurrent();
            Gl.glViewport(simpleOpenGlControl1.ClientRectangle.X, simpleOpenGlControl1.ClientRectangle.Y, simpleOpenGlControl1.ClientRectangle.Width, simpleOpenGlControl1.ClientRectangle.Height);
            float m_AspectRatio = (float)simpleOpenGlControl1.Width / (float)simpleOpenGlControl1.Height;
            Gl.glMatrixMode(Gl.GL_PROJECTION);
            Gl.glLoadIdentity();
            Matrix4 projmtx = Matrix4.CreatePerspectiveFieldOfView((float)((70.0f * Math.PI) / 180.0f), m_AspectRatio, 0.01f, 1000.0f);
            Gl.glMultMatrixf(projmtx.ToFloat());*/
            simpleOpenGlControl1.Refresh();
            //RenderFunc1();
        }

        List<int[]> aniframeS = new List<int[]>();
        List<int[]> aniframeT = new List<int[]>();
        List<int[]> aniScaleS = new List<int[]>();
        List<int[]> aniScaleT = new List<int[]>();
        List<int[]> aniframeR = new List<int[]>();
        private void timer1_Tick_1(object sender, EventArgs e)
        {
            simpleOpenGlControl1.Tag = true;
            simpleOpenGlControl1.Refresh();
            simpleOpenGlControl1.Tag = false;
        }

        private void nsbmd_FormClosing(object sender, FormClosingEventArgs e)
        {
            timer1.Stop();
        }
        byte[] inttobyte(int a)
        {
            byte[] buffer;
            buffer = System.BitConverter.GetBytes(a);
            return buffer;
        }

        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            simpleOpenGlControl1.Refresh();//.Invalidate();
            //RenderFunc1();
        }

        private void simpleOpenGlControl1_Paint(object sender, PaintEventArgs e)
        {
            RenderFunc1((bool)simpleOpenGlControl1.Tag);
            //simpleOpenGlControl1.MakeCurrent();

            //simpleOpenGlControl1.SwapBuffers();
        }

        private void simpleOpenGlControl1_Load(object sender, EventArgs e)
        {
            //Gl.glEnable(Gl.GL_DITHER);
            //Gl.glEnable(Gl.GL_LINE_SMOOTH);
            Gl.glEnable(Gl.GL_POLYGON_SMOOTH);
            Gl.glEnable(Gl.GL_DEPTH_TEST);
            Gl.glEnable(Gl.GL_TEXTURE_2D);
            Gl.glEnable(Gl.GL_DEPTH);

            Gl.glEnable(Gl.GL_ALPHA_TEST);

            Gl.glBlendFunc(Gl.GL_SRC_ALPHA, Gl.GL_ONE_MINUS_SRC_ALPHA);
            Gl.glEnable(Gl.GL_BLEND);
            Gl.glAlphaFunc(Gl.GL_GREATER, 0f);
            /*simpleOpenGlControl1.MakeCurrent();
            Gl.glViewport(simpleOpenGlControl1.ClientRectangle.X, simpleOpenGlControl1.ClientRectangle.Y, simpleOpenGlControl1.ClientRectangle.Width, simpleOpenGlControl1.ClientRectangle.Height);
            float m_AspectRatio = (float)simpleOpenGlControl1.Width / (float)simpleOpenGlControl1.Height;
            Gl.glMatrixMode(Gl.GL_PROJECTION);
            Gl.glLoadIdentity();
            Matrix4 projmtx = Matrix4.CreatePerspectiveFieldOfView((float)((70.0f * Math.PI) / 180.0f), m_AspectRatio, 0.01f, 1000.0f);
            Gl.glMultMatrixf(projmtx.ToFloat());
            Gl.glEnable(Gl.GL_POLYGON_SMOOTH);
            Gl.glEnable(Gl.GL_DEPTH_TEST);
            Gl.glEnable(Gl.GL_TEXTURE_2D);
            Gl.glEnable(Gl.GL_ALPHA_TEST);
            Gl.glBlendFunc(Gl.GL_SRC_ALPHA, Gl.GL_ONE_MINUS_SRC_ALPHA);
            Gl.glEnable(Gl.GL_BLEND);
            Gl.glAlphaFunc(Gl.GL_GREATER, 0f);
            ReadCourseModel();
            ReadVModel();
            ReadNKM();
            ParseNKM();
            //Gl.glCallList(LevelModel);
            if (timer1.Enabled)
            {
                timer1.Start();
            }
            simpleOpenGlControl1.Refresh();*/
        }
        int selected = -1;
        private void simpleOpenGlControl1_MouseUp(object sender, MouseEventArgs e)
        {
            //Vector3 coord = Get3DCoords(e.Location, 2f);
            /*double[] mm = new double[16];
            Gl.glGetDoublev(Gl.GL_MODELVIEW_MATRIX, mm);
            double[] pm = new double[16];
            Gl.glGetDoublev(Gl.GL_PROJECTION_MATRIX, pm);
            int[] vp = new int[4];
            Gl.glGetIntegerv(Gl.GL_VIEWPORT, vp);
            float Z = -1;
            float winy = vp[3] - e.Y;
            Gl.glReadPixels(e.X, (int)winy, 1, 1, Gl.GL_DEPTH_COMPONENT, Gl.GL_FLOAT, Z);

            double x;
            double y;
            double z;
            Glu.gluUnProject(e.X, (int)winy, Z, mm, pm, vp, out x, out y, out z);
            for (int i = (File.Exists(path + "\\" + "unpack\\" + "course_model_V.nsbmd") ? 1 : 2); i < nsbm.Count; i++)
            {
                if (obj[i] is nkm.OBJi)
                {
                    nkm.OBJi f = obj[i] as nkm.OBJi;
                    System.Windows.Media.Media3D.Rect3D r = new System.Windows.Media.Media3D.Rect3D();
                    r.X = f.TransX / getScale();// -nsbm[i].models[0].boundXmin / 2;
                    r.Y = f.TransY / getScale();// -nsbm[i].models[0].boundYmin / 2;
                    r.Z = f.TransZ / getScale();// -nsbm[i].models[0].boundZmin / 2;
                    r.SizeX = nsbm[i].models[0].boundXmax;
                    r.SizeY = nsbm[i].models[0].boundYmax;
                    r.SizeZ = nsbm[i].models[0].boundZmax;
                    r.SizeX *= (f.ScaleX / getScale2(BitConverter.ToString(f.ObjectID).Replace("-", "")));
                    r.SizeY *= (f.ScaleY / getScale2(BitConverter.ToString(f.ObjectID).Replace("-", "")));
                    r.SizeZ *= (f.ScaleZ / getScale2(BitConverter.ToString(f.ObjectID).Replace("-", "")));
                    if (r.Contains(x, y, z))
                    {
                        selected = i;
                        OBJIProperty p = new OBJIProperty();
                        p.X = f.TransX;
                        p.Y = f.TransY;
                        p.Z = f.TransZ;
                        p.RotationX = f.RotX;
                        p.RotationY = f.RotY;
                        p.RotationZ = f.RotZ;
                        p.ScaleX = f.ScaleX;
                        p.ScaleY = f.ScaleY;
                        p.ScaleZ = f.ScaleZ;
                        p.Object = f.ObjectID;
                        p.Setting1 = f.Setting1;
                        p.Setting2 = f.Setting2;
                        p.Setting3 = f.Setting3;
                        p.Setting4 = f.Setting4;
                        p.Setting5 = f.Setting5[0] == 1;
                        propertyGrid1.SelectedObject = p;//obj[i];
                        simpleOpenGlControl1.Refresh();
                        //MessageBox.Show("");
                        break;
                    }
                    else
                    {
                        selected = -1;
                        propertyGrid1.SelectedObject = new object();
                        simpleOpenGlControl1.Refresh();
                    }
                }
            }*/
            int s = (int)m_PickingFrameBuffer[0];  //Color.FromArgb((int)m_PickingFrameBuffer[0], (int)m_PickingFrameBuffer[1], (int)m_PickingFrameBuffer[2]).ToArgb();
            if (s != 0)
            {
                if (obj[s] is nkm.OBJi)
                {
                    nkm.OBJi f = obj[s] as nkm.OBJi;
                    selected = s;
                    OBJIProperty p = new OBJIProperty();
                    p.X = f.TransX;
                    p.Y = f.TransY;
                    p.Z = f.TransZ;
                    p.RotationX = f.RotX;
                    p.RotationY = f.RotY;
                    p.RotationZ = f.RotZ;
                    p.ScaleX = f.ScaleX;
                    p.ScaleY = f.ScaleY;
                    p.ScaleZ = f.ScaleZ;
                    p.Object = f.ObjectID;
                    p.Setting1 = f.Setting1;
                    p.Setting2 = f.Setting2;
                    p.Setting3 = f.Setting3;
                    p.Setting4 = f.Setting4;
                    p.Setting5 = f.Setting5[0] == 1;
                    propertyGrid1.SelectedObject = p;
                    simpleOpenGlControl1.Refresh();
                }
            }
        }

        private class OBJIProperty
        {
            public float X { get; set; }
            public float Y { get; set; }
            public float Z { get; set; }
            public float RotationX { get; set; }
            public float RotationY { get; set; }
            public float RotationZ { get; set; }
            public float ScaleX { get; set; }
            public float ScaleY { get; set; }
            public float ScaleZ { get; set; }
            public byte[] Object { get; set; }
            public byte[] Setting1 { get; set; }
            public byte[] Setting2 { get; set; }
            public byte[] Setting3 { get; set; }
            public byte[] Setting4 { get; set; }
            public bool Setting5 { get; set; }
        }

        private void simpleOpenGlControl1_MouseMove(object sender, MouseEventArgs e)
        {
            simpleOpenGlControl1.Refresh();
        }

        private void treeView1_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
        {
            selected = treeView1.Nodes.IndexOf(e.Node) + (File.Exists(path + "\\" + "unpack\\" + "course_model_V.nsbmd") ? 1 : 2);
            simpleOpenGlControl1.Refresh();
        }

    }
}
